package com.cqit.bluetoothkeybox.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtil {

    public static final String STANDARD_FORMAT = "yyyy-MM-dd";

    /**
     * @return
     * @Description: 获得当前时间的周日的日期
     */
    public static String getSunday() {
        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_FORMAT);
        // 这种输出的是上个星期周日的日期，因为老外那边把周日当成第一天
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        // 增加一个星期，才是我们中国人理解的本周日的日期
        calendar.add(Calendar.WEEK_OF_YEAR, 1);
        //获取本周日的日期
        return sdf.format(calendar.getTime());
    }

    /**
     * @return
     * @Description: 获得当前时间的周一的日期
     */
    public static String getMonday() {
        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_FORMAT);
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //
        //获取本周一的日期
        return sdf.format(calendar.getTime());
//        return calendar.getTime();
    }

    /**
     * @return
     * @Description: 获得指定月份的日期最后一天日期
     */
    public static String getMonthLastday(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_FORMAT);

        calendar.set(Calendar.YEAR, year);
        calendar.add(Calendar.MONTH, month - getCurrentMonth());
        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return sdf.format(calendar.getTime());
//        return calendar.getTime();
    }

    /**
     * @return
     * @Description: 获得指定月份的日期第一天日期
     */
    public static String getMonthFirstday(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_FORMAT);

        calendar.set(Calendar.YEAR, year);
        calendar.add(Calendar.MONTH, month - getCurrentMonth());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return sdf.format(calendar.getTime());
//        return calendar.getTime();
    }

    /**
     * @return
     * @Description: 获取当前时间的年份
     */
    public static Integer getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.YEAR);
    }

    /**
     * @return
     * @Description: 获取当前时间的月份
     */
    public static Integer getCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        Integer currentMonth = calendar.get(Calendar.MONTH) + 1;
        return currentMonth;
    }

    /**
     * @return java.lang.Integer
     * @Description 获取当月天数
     * @Param []
     **/
    public static Integer getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * @param calendar
     * @param type     0开始时间000，1结束时间235959
     * @param day      设置日数
     */
    private static void setTime(Calendar calendar, int type, int day) {
        calendar.set(Calendar.DAY_OF_MONTH, day);
        if (type == 0) {
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 0);
        }

    }

    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar now = Calendar.getInstance();
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        now.setTime(nowTime);
        start.setTime(startTime);
        end.setTime(endTime);

        return now.after(start) && now.before(end);
    }

}
